<template>
  <div>
    <div v-if="mode === 'DESIGN'">
      <el-date-picker disabled :type="type" :placeholder="$t(placeholder)"></el-date-picker>
    </div>
    <div v-else-if="mode === 'PC' && !readonly" >
      <el-date-picker v-model="_value" :value-format="format" :format="format" clearable :type="type" :placeholder="$t(placeholder)" style="width:100%"></el-date-picker>
    </div>
    <div v-else-if="mode === 'MOBILE' && !readonly">
      <field readonly right-icon="arrow" clickable name="datetimePicker" :model-value="_value" :placeholder="$t(placeholder)" @click="showPicker = true"/>
      <popup v-model:show="showPicker" position="bottom">
        <picker-group :title="desc" :tabs="[$t('webAuto.SelectDate'), $t('webAuto.Timing')]" v-if="type === 'datetime'" @confirm="onConfirm">
          <date-picker :columns-type="_type" :formatter="formatter" v-model="currentDate"/>
          <time-picker :formatter="formatter" v-model="currentTime" />
        </picker-group>
        <date-picker v-else :columns-type="_type" @confirm="onConfirm" :formatter="formatter" v-model="currentDate"/>
      </popup>
    </div>
    <div v-else>
      <!-- {{ _value }} -->
      <el-date-picker style="width:100%" v-model="_value" :value-format="format" :format="format" clearable :type="type" :disabled="readonly"></el-date-picker>
    </div>
  </div>
</template>

<script>
import { Field, Popup, DatePicker, TimePicker, PickerGroup } from 'vant'
import componentMinxins from '../ComponentMinxins'
import moment from 'moment'

export default {
  mixins: [componentMinxins],
  name: 'DateTime',
  components: { Field, Popup, DatePicker, TimePicker, PickerGroup },
  props: {
    modelValue: {
      default: null,
    },
    format: {
      type: String,
      default: 'YYYY-MM-DD HH:mm',
    },
    placeholder: {
      type: String,
      default: 'webAuto.selectDate',
    },
  },
  computed: {
    type() {
      switch (this.format) {
        case 'YYYY':
          return 'year'
        case 'YYYY-MM':
          return 'month'
        case 'YYYY-MM-DD':
          return 'date'
        case 'YYYY-MM-DD HH:mm':
          return 'datetime'
        case 'YYYY-MM-DD HH:mm:ss':
          return 'datetime'
        default:
          return 'datetime'
      }
    },
    _type(){
      switch (this.format) {
        case 'YYYY':
          return ['year']
        case 'YYYY-MM':
          return ['year', 'month']
        case 'YYYY-MM-DD':
          return ['year', 'month', 'day']
        default:
          return ['year', 'month', 'day']
      }
    },
    desc() {
      switch (this.format) {
        case 'YYYY':
          return this.$t('webAuto.SelectYear')
        case 'YYYY-MM':
          return this.$t('webAuto.SelectYearMonth')
        case 'YYYY-MM-DD':
          return this.$t('webAuto.SelectYearMonthDay')
        case 'YYYY-MM-DD HH:mm':
          return this.$t('webAuto.SelectYearAll')
        default:
          return this.$t('webAuto.SelectYearAll')
      }
    },
    timeVal: {
      get() {
        if (this.$isNotEmpty(this.modelValue)) {
          return moment(this.modelValue)
        } else {
          return new Date()
        }
      },
      set(val) {
        this.$emit('update:modelValue', this.getFormatVal(val))
      },
    },
  },
  data() {
    return {
      showPicker: false,
      currentDate: [],
      currentTime: []
    }
  },
  methods: {
    onConfirm(val) {
      this.showPicker = false
      this._value = this.getFormatVal(val)
    },
    getFormatVal(val) {
      if (this.type === 'datetime') {
        return `${val[0].selectedValues.join('-')} ${val[1].selectedValues.join(':')}`
      }
      return val.selectedValues.join('-')
    },
    formatter(type, option) {
      switch (type){
        case 'year': option.text += this.$t('webAuto.year'); break;
        case 'month': option.text += this.$t('webAuto.Moon'); break;
        case 'day': option.text += this.$t('webAuto.sun'); break;
        case 'hour': option.text += this.$t('webAuto.time'); break;
        case 'minute': option.text += this.$t('webAuto.divide'); break;
      }
      return option
    },
    filter(type, options) {
      /*if (type === 'minute') {
      return options.filter((option) => option % 5 === 0);
    }*/
      return options
    },
  },
}
</script>

<style scoped></style>
